package br.uefs.dexa.rfframework.gui.impl;

import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;

import br.uefs.dexa.rfframework.gui.interfaces.DigitalObjectPanel;
import br.uefs.dexa.rfframework.gui.interfaces.QueryObjectSetContainerPanel;
import br.uefs.dexa.rfframework.gui.interfaces.RFUserGUI;
import br.uefs.dexa.rfframework.gui.utils.WrapLayout;
import br.unicamp.ic.lis.rfframework.objects.IDigitalObject;

public class ImageQueryPatternPanel extends QueryObjectSetContainerPanel {

	
	private static final long serialVersionUID = 6322961282238542892L;
	
	private static final int SCROLL_BAR_UNIT_INCREMENT = 35;
	
	private JPanel queryImagesShowPanel;
	private JPanel buttonsPanel;	
	private JButton runButton;
	private JScrollPane queryScrollPane;
	
	
	public ImageQueryPatternPanel(RFUserGUI gui) 
	{
		super(gui);
	}

	@Override
	public void compose() 
	{
		queryImagesShowPanel = new JPanel();
		buttonsPanel = new JPanel(); 
		this.runButton = new JButton("Run");
		this.runButton.setEnabled(false);
		buildPanel();

	}
	
	private void buildPanel() 
	{
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		queryImagesShowPanel.setLayout(new WrapLayout(FlowLayout.LEFT));
		//queryImagesShowPanel.setPreferredSize(new Dimension(400, 260 ));
		//queryImagesShowPanel.setMinimumSize(new Dimension(400, 260 ));
		
		queryScrollPane = new JScrollPane(queryImagesShowPanel);
		queryScrollPane.getVerticalScrollBar().setUnitIncrement(SCROLL_BAR_UNIT_INCREMENT);
		
		buttonsPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		buttonsPanel.add(runButton);	
		//buttonsPanel.add(statusLabel);
		
		add(queryScrollPane);
		add(buttonsPanel);	
	}
	
	

	@Override
	public Component getQueryTriggerComponent() 
	{
		return runButton;
	}

	@Override
	public Container getQueryObjectsContainer() 
	{
		return queryImagesShowPanel; 
	}
	
	@Override
	public void afterQuerySelection()
	{
		this.runButton.setEnabled(false);
		runButton.validate();
		runButton.repaint();
	}
	
	@Override
	public void addObject(IDigitalObject object)
	{
		super.addObject(object);
		if(objects.getSize() > 0)
			runButton.setEnabled(true);
	}
	
	@Override
	public void removeObject(IDigitalObject object)
	{
		super.removeObject(object);
		if(objects.getSize() == 0)
			runButton.setEnabled(false);
	}
	
	@Override
	public void showObjectPanel(DigitalObjectPanel panel)
	{
		super.showObjectPanel(panel);
//		if(getPanelsSize() > 0)
//			runButton.setEnabled(true);
		
		JScrollBar vertical = queryScrollPane.getVerticalScrollBar();
		vertical.setValue( vertical.getMaximum() );
	}

	@Override
	public void removeObjectPanel(DigitalObjectPanel panel)
	{
		super.removeObjectPanel(panel);
		if(getPanelsSize() == 0)
			runButton.setEnabled(false);
		
		JScrollBar vertical = queryScrollPane.getVerticalScrollBar();
		vertical.setValue( vertical.getMaximum() );
	}

}
